<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');

class News extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index() {
		//$this->load->view('welcome_message');
		/*$this -> load -> model('Torneo_Model');

		 $results = $this -> Torneo_Model -> get_tournaments_active();

		 $data['tournaments'] = $results;

		 $this -> load -> view('league_main' , $data);*/
	}

	public function admon() {
		
		$this -> is_logged_in();
		$this -> load -> view('admin_news_view');

	}

	public function create() {

		$data['team_name'] = array('name' => 'title', 'id' => 'title', 'size' => '60');
		$this -> load -> view('news_create_view', $data);
	}

	public function view($news_id =FALSE) {

		if($news_id && is_numeric($news_id)) {

			$query = $this -> db -> query('SELECT * FROM blog_news where news_id=' . $news_id . ';');
			$results = $query -> result();

			$data['content'] = $results[0] -> content;
			$data['title'] = $results[0] -> title;
			$data['date'] = $results[0] -> date;
			$data['user'] = $results[0] -> user;
			$data['news_id'] = $results[0] -> news_id;
			$data['allow_comments'] = $results[0] -> allow_comments;

			//print_r($data);

			$this -> load -> view('news_show_view', $data);
			//$data['team_name'] = array('name' => 'title', 'id' => 'title', 'size' => '60');
			//$this -> load -> view('news_create_view', $data);
		}

	}

	public function edit($news_id =FALSE) {

		if($news_id && is_numeric($news_id)) {

			$query = $this -> db -> query('SELECT * FROM blog_news where news_id=' . $news_id . ';');
			$results = $query -> result();

			$data['content'] = $results[0] -> content;
			$data['title'] = $results[0] -> title;
			$data['date'] = $results[0] -> date;
			$data['user'] = $results[0] -> user;
			$data['news_id'] = $results[0] -> news_id;
			$data['allow_comments'] = $results[0] -> allow_comments;

			//print_r($data);
			$data['title_in'] = array('name' => 'title', 'id' => 'title', 'size' => '60', 'value' => $data['title']);

			$this -> load -> view('news_edit_view', $data);
			//$data['team_name'] = array('name' => 'title', 'id' => 'title', 'size' => '60');
			//$this -> load -> view('news_create_view', $data);
		}

	}

	public function submit() {

		print_r($_POST);

		if(!isset($_POST['user'])) {

			$_POST['user'] = 'admin';

		}

		if(!isset($_POST['allow_comments'])) { $allow_comments = 0;} else { $allow_comments = 1; }

		$data = array('title' => $_POST['title'], 'content' => $_POST['content'], 'user' => $_POST['user'], 'allow_comments' => $allow_comments);

		$this -> db -> insert('blog_news', $data);

	}

	public function save() {

		print_r($_POST);

		//if(!isset($_POST['user'])) {$_POST['user'] = 'admin';}
		
		if(!isset($_POST['user'])) {$_POST['user'] = 'admin';}
		
		if(!isset($_POST['allow_comments'])) { $allow_comments = 0;} else { $allow_comments = 1; }
		
		//if(!isset($_POST('allow_comments'))){$_POST['allow_comments']=false; } else {$_POST['allow_comments']=false;}

		$data = array(  'title' => $_POST['title'], 
						'content' => $_POST['content'], 
						'user' => $_POST['user'], 
						'allow_comments' => $allow_comments );
						
		

		//$this -> db -> insert('blog_news', $data);
		
		$this -> db -> where('news_id', $_POST['news_id']);
		$this -> db -> update('blog_news', $data);

	}
	
	function is_logged_in()
	{
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			echo 'No Cuentas con permisos para acceder a esta pagina. <a href="/index.php/login">Entrar</a>';	
			die();		
			//$this->load->view('login_form');
		}		
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
